#include "mygview.h"

MyGView::MyGView(QWidget *parent) :
    QGraphicsView(parent)
{
  isScale=false;
  anothercursor= false;
  this->setRenderHint(QPainter::Antialiasing,true);
}

void MyGView::resizeEvent(QResizeEvent * event){
    //Get the rectangle of the visible area in scene coords
  QRectF visibleArea = mapToScene(rect()).boundingRect();
  SetCenter(visibleArea.center());

   //Call the subclass resize so the scrollbars are updated correctly
  QGraphicsView::resizeEvent(event);
}

void MyGView::mousePressEvent(QMouseEvent * event){
  if ( !anothercursor ){
       LastPanPoint = event->pos();
       this->viewport()->setCursor(Qt::ClosedHandCursor);
  }else{
      QPoint *pos = new QPoint( QPointF(mapToScene(event->pos())).toPoint() );
      emit gvPress( pos );
      pos = NULL;
      delete pos;
  }
}

void MyGView::mouseReleaseEvent(QMouseEvent * event){
  if ( !anothercursor ){
   this->viewport()->setCursor(Qt::OpenHandCursor);
   LastPanPoint = QPoint();
  }
}

void MyGView::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent){
    //Get the position of the mouse before scaling, in scene coords
    scale(10, 10);
    QPointF pointBeforeScale(mapToScene(mouseEvent->pos().toPoint()));

    //Get the original screen centerpoint
    QPointF screenCenter = GetCenter(); //CurrentCenterPoint; //(visRect.center());

    //Scale the view ie. do the zoom
    double scaleFactor = 2; //How fast we zoom

    if(!isScale) {
        //Zoom in
        scale(scaleFactor, scaleFactor);
        isScale=true;
    } else {
        //Zooming out
        scale(1.0 / scaleFactor, 1.0 / scaleFactor);
        isScale=false;
    }

    //Get the position after scaling, in scene coords
    QPointF pointAfterScale(mapToScene(mouseEvent->pos().toPoint()));

    //Get the offset of how the screen moved
    QPointF offset = pointBeforeScale - pointAfterScale;

    //Adjust to the new center for correct zooming
    QPointF newCenter = screenCenter + offset;
    SetCenter(newCenter);
}

/**
*Handles the mouse move event
*/
void MyGView::mouseMoveEvent(QMouseEvent* event) {

    if(!LastPanPoint.isNull()) {
        //Get how much we panned
        QPointF delta = mapToScene(LastPanPoint) - mapToScene(event->pos());
        LastPanPoint = event->pos();

        //Update the center ie. do the pan
        SetCenter(GetCenter() + delta);
    }

}

/**
  * Sets the current centerpoint.  Also updates the scene's center point.
  * Unlike centerOn, which has no way of getting the floating point center
  * back, SetCenter() stores the center point.  It also handles the special
  * sidebar case.  This function will claim the centerPoint to sceneRec ie.
  * the centerPoint must be within the sceneRec.
  */
//Set the current centerpoint in the
void MyGView::SetCenter(const QPointF& centerPoint) {
    //Get the rectangle of the visible area in scene coords
    QRectF visibleArea = mapToScene(rect()).boundingRect();

    //Get the scene area
    QRectF sceneBounds = sceneRect();


    double boundX = visibleArea.width() / 2.0;
    double boundY = visibleArea.height() / 2.0;
    double boundWidth = sceneBounds.width() - 2.0 * boundX;
    double boundHeight = sceneBounds.height() - 2.0 * boundY;

    //The max boundary that the centerPoint can be to
    QRectF bounds(boundX, boundY, boundWidth, boundHeight);

    if(bounds.contains(centerPoint)) {
        //We are within the bounds
        CurrentCenterPoint = centerPoint;
    } else {
        //We need to clamp or use the center of the screen
        if(visibleArea.contains(sceneBounds)) {
            //Use the center of scene ie. we can see the whole scene
            CurrentCenterPoint = sceneBounds.center();
        } else {

            CurrentCenterPoint = centerPoint;

            //We need to clamp the center. The centerPoint is too large
            if(centerPoint.x() > bounds.x() + bounds.width()) {
                CurrentCenterPoint.setX(bounds.x() + bounds.width());
            } else if(centerPoint.x() < bounds.x()) {
                CurrentCenterPoint.setX(bounds.x());
            }

            if(centerPoint.y() > bounds.y() + bounds.height()) {
                CurrentCenterPoint.setY(bounds.y() + bounds.height());
            } else if(centerPoint.y() < bounds.y()) {
                CurrentCenterPoint.setY(bounds.y());
            }

        }
    }

    //Update the scrollbars
    centerOn(CurrentCenterPoint);
}

/**
  * Zoom the view in and out.
  */
void MyGView::wheelEvent(QWheelEvent* event) {

    //Get the position of the mouse before scaling, in scene coords
    QPointF pointBeforeScale(mapToScene(event->pos()));

    //Get the original screen centerpoint
    QPointF screenCenter = GetCenter(); //CurrentCenterPoint; //(visRect.center());

    //Scale the view ie. do the zoom
    double scaleFactor = 1.15; //How fast we zoom
    if(event->delta() > 0) {
        //Zoom in
        scale(scaleFactor, scaleFactor);
    } else {
        //Zooming out
        scale(1.0 / scaleFactor, 1.0 / scaleFactor);
    }

    //Get the position after scaling, in scene coords
    QPointF pointAfterScale(mapToScene(event->pos()));

    //Get the offset of how the screen moved
    QPointF offset = pointBeforeScale - pointAfterScale;

    //Adjust to the new center for correct zooming
    QPointF newCenter = screenCenter + offset;
    SetCenter(newCenter);
}


void MyGView::keyPressEvent(QKeyEvent *keyEvent){
    QPoint *np = new QPoint() ;
    np->operator =( GetCenter().toPoint() );

    int step;
    step = 60;

    switch (keyEvent->key()) {
            case Qt::Key_Left:
                    if ( keyEvent->modifiers()==Qt::ControlModifier ){
                        //emit changePix(-1);
                    }else
                    np->setX( np->x()- step);
                    break;
            case Qt::Key_Right:
                    if ( keyEvent->modifiers()==Qt::ControlModifier ){
                        //emit changePix(1);
                    }else
                      np->setX( np->x()+ step);
                    break;
            case Qt::Key_Up:
                    np->setY( np->y()- step);
                    break;
            case Qt::Key_Down:
                    np->setY( np->y()+ step);
                    break;
            case Qt::Key_Plus:
                    this->scale(2, 2);
                    break;
            case Qt::Key_Minus:
                    this->scale(0.5, 0.5);
                    break;
            case Qt::Key_F11:
                    emit hidePanel();
                    break;
            case Qt::Key_Escape:
                    emit escapePress();
                    break;

    }
    SetCenter( *np );
    np = NULL;
    delete np;

}

//кода переносим файлы
void MyGView::dropEvent(QDropEvent *event){

    const QMimeData* mimeData = event->mimeData();
    QStringList *files;
    files = new QStringList();
    if (  mimeData->hasUrls() ){
      for ( int i=0; i<mimeData->urls().size(); i++ ) {
          files->append(mimeData->urls().at(i).toString().replace("file:///",""));
      }
    }
    emit dropFiles(files);
    files = NULL;
    delete files;
    mimeData = NULL;
    delete mimeData;
    event->accept();

}

void MyGView::setNull(){
   setSceneRect(0,0,0,0);
   SetCenter(QPoint(0,0) );
}



